package com.cskaoyan.market.admin.controller;


import com.cskaoyan.market.admin.service.impl.MarketCouponServiceImpl;
import com.cskaoyan.market.db.domain.MarketCoupon;
import com.cskaoyan.market.util.GetReqBodyUtil;
import com.cskaoyan.market.util.JacksonUtil;
import com.cskaoyan.market.util.ResponseUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;

@RestController
@RequestMapping("/admin/coupon")
public class AdminCouponController{
    @Autowired
    MarketCouponServiceImpl couponService;

    @GetMapping("/read")
    private void doRead(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        ServletInputStream inputStream = req.getInputStream();
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        int length = 0;
        byte[] bytes = new byte[1024];
        while ((length = inputStream.read(bytes)) != -1) {
            byteArrayOutputStream.write(bytes, 0, length);
        }
        String requestBody = byteArrayOutputStream.toString("utf-8");
        String couponId = req.getParameter("id");
        Integer id = null;
        if (couponId!=null){
            id= Integer.valueOf(couponId);
        }
        MarketCoupon marketTopics = couponService.read(id);
        Object ok=ResponseUtil.ok(marketTopics);
        ObjectMapper objectMapper=JacksonUtil.getObjectMapper();
        String s = objectMapper.writeValueAsString(ok);
        resp.getWriter().println(s);

    }

    @GetMapping("/listuser")
    private void listUser(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        ServletInputStream inputStream = req.getInputStream();
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        int length = 0;
        byte[] bytes = new byte[1024];
        while ((length = inputStream.read(bytes)) != -1) {
            byteArrayOutputStream.write(bytes, 0, length);
        }
        String requestBody = byteArrayOutputStream.toString("utf-8");
        String couponId = req.getParameter("couponId");
        couponService.listUser(resp);

    }

    @GetMapping("/list")
    private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //具体的业务逻辑，不要写在controller层中，而是写在service层中
        //该接口其实有很多种实现方案：1.使用数据库层面的连接查询  2.先简单查询一次，得到了list，在java语言中进行组装适配，如果两种方案你都想去实现一遍，那么可以写在两个不同service方法中
        Integer page= Integer.valueOf(req.getParameter("page"));
        Integer limit= Integer.valueOf(req.getParameter("limit"));
        String name = req.getParameter("name");
        String s = req.getParameter("status");
        Integer status=null;
        if (s!=null){
            status=Integer.valueOf(s);
        }
        String sort = req.getParameter("sort");
        String order = req.getParameter("order");

        List<MarketCoupon> coupons = couponService.doList(page,limit,name,status,sort,order);
        //返回响应
        Object o = ResponseUtil.okList(coupons);
        resp.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(o));
    }

    @PostMapping("/update")
    private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        ServletInputStream inputStream = req.getInputStream();
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        int length = 0;
        byte[] bytes = new byte[1024];
        while ((length = inputStream.read(bytes)) != -1) {
            byteArrayOutputStream.write(bytes, 0, length);
        }
        String requsetBody = byteArrayOutputStream.toString("utf-8");
        String name = JacksonUtil.parseString(requsetBody, "name");
        String desc = JacksonUtil.parseString(requsetBody, "desc");
        String tag = JacksonUtil.parseString(requsetBody, "tag");
        BigDecimal min = new BigDecimal(JacksonUtil.parseString(requsetBody, "min"));
        Short limit = new Short(JacksonUtil.parseString(requsetBody, "limit"));
        Integer total = Integer.valueOf(JacksonUtil.parseString(requsetBody, "total"));
        Short days = Short.valueOf(JacksonUtil.parseString(requsetBody, "days"));
        Integer id = Integer.valueOf(JacksonUtil.parseString(requsetBody, "id"));
        Short  type = Short.valueOf(JacksonUtil.parseString(requsetBody, "type"));

        MarketCoupon marketTopics = couponService.update(name, desc, tag, min, limit, total, days, id, type);
        Object ok=ResponseUtil.ok(marketTopics);
        ObjectMapper objectMapper=JacksonUtil.getObjectMapper();
        String s = objectMapper.writeValueAsString(ok);
        resp.getWriter().println(s);
    }


    @PostMapping("/delete")
    private void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        BufferedReader bufferedReaderRequestBody = req.getReader();
        String requestBody = bufferedReaderRequestBody.readLine();
        MarketCoupon marketCoupon = JacksonUtil.mapper.readValue(requestBody, MarketCoupon.class);
        Integer id = marketCoupon.getId();
        int i=couponService.delete(id);
        if (i>0){
            Object ok = ResponseUtil.ok();
            ObjectMapper objectMapper = JacksonUtil.getObjectMapper();
            String s = objectMapper.writeValueAsString(ok);
            resp.getWriter().println(s);
        }
    }

    @PostMapping("/create")
    private void create(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String body = GetReqBodyUtil.getRequestBody(req);
        MarketCoupon marketCoupon = JacksonUtil.getObjectMapper().readValue(body, MarketCoupon.class);
        MarketCoupon respMarketCoupon = couponService.create(marketCoupon);
        resp.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(ResponseUtil.ok(respMarketCoupon)));
    }
}
